<template>
  <div class="overhaulApplication h100">
    <list-table-new v-bind="tableSetting" ref="klTable"> </list-table-new>
  </div>
</template>
<script>
import { queryShiftRecordList } from "@/api/operationManagement/handoverManagement.js";
export default {
  data() {
    return {
      searchData: {},
      tableSetting: {
        radioShow: true,
        selectionShow: false,
        pageConfig: {
          pageCode: "handoverRecord",
          userId: this.$store.getters.userInfo.namePy,
        },
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
            deptId: this.$store.getters.currentInfo.currOrg,
            deptName: this.$store.getters.currentInfo.currOrgName
          },
          searchArr: [
            {
              type: "select",
              label: "人员类型",
              model: "staffTypes",
              multiple: true,
              option: this.$store.getters.dirtData("operation_staff_type"),
            },
            {
              type: "organization",
              label: "值班单位",
              model: "deptName",
              idsStr: "deptId",
            },
            {
              type: "location",
              label: "值班地点",
              model: "locationName",
              idsStr: "locationId",
            },
            {
              type: "datetimerange",
              label: "值班时间",
              model: "startDates",
              placeholder: "请输入",
            },
          ],
        },
        //过滤器的值
        filterFormData: {},
        //过滤器的默认值
        defaultFilterData: {},
        userCustomData: [],
        actionList: [
          {
            name: "导出",
            icon: "el-icon-download",
            disabled: !this.$store.getters.isBtnAuth("export"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.download();
            },
          },
          {
            name: "补录",
            icon: "el-icon-plus",
            disabled: !this.$store.getters.isBtnAuth("supplyRecord"), //控制按钮的禁用
            content: "未授权功能", //禁用提示内容
            method: (row) => {
              this.makeUp();
            },
          },
        ],
        column: [
          {
            label: "值班单位",
            prop: "deptName",
          },
          {
            label: "值班地点",
            prop: "locationName",
          },
          {
            label: "本值",
            subColumn: [
              {
                label: "班次",
                prop: "thisName",
              },
              {
                label: "班次开始时间",
                prop: "thisStartTime",
              },
              {
                label: "班次结束时间",
                prop: "thisEndTime",
              },
              {
                label: "值班人员",
                prop: "thisStaffs",
                formatter: (row) => {
                  return this.handleMemberInfo(row.thisStaffs);
                },
              },
              {
                label: "交班时间",
                prop: "thisTime",
              },
            ],
          },
          {
            label: "下值",
            subColumn: [
              {
                label: "班次",
                prop: "nextName",
              },
              {
                label: "班次开始时间",
                prop: "nextStartTime",
              },
              {
                label: "班次结束时间",
                prop: "nextEndTime",
              },
              {
                label: "值班人员",
                prop: "nextStaffs",
                formatter: (row) => {
                  return this.handleMemberInfo(row.nextStaffs);
                },
              },
              {
                label: "接班时间",
                prop: "nextTime",
              },
            ],
          },
        ],
        tableData: [],
        paginationObj: {
          pageSizes: [15, 30, 50, 100],
        },
        totalNumber: 0,
        //表格方法
        tableMethods: {
          //点击行的方法
          rowClick: (row) => {
            let params = {
              shiftId: row.thisShiftId,
              nextShiftId: row.nextShiftId,
            };
            this.goDetailPage(params, "handoverDetails", "交接班记录");
          },
        },
      },
    };
  },
  created() {
    /* 添加监听事件 */
    window.addEventListener("storage", this.updateData);
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.updateData);
  },
  mounted() {
    this.$refs.klTable.getTableConfig();
    this.updateData();
  },
  methods: {
    updateData() {
      this.$refs.klTable.searchData();
    },
    // 获取表格数据
    getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      this.searchData = params;
      if (sortStr.length > 0) {
        queryData.orders = sortStr;
      }
      queryShiftRecordList(queryData).then((res) => {
        this.$set(this.tableSetting, "tableData", res.data.list || []);
        this.$set(this.tableSetting, "totalNumber", res.data.total);
      });
    },
    //处理人员数据
    handleMemberInfo(data) {
      let str = "";
      data.forEach((item) => {
        if (item.type == "0") {
          str += item.staffName + "|";
        } else {
          str += item.staffName + ",";
        }
      });
      str = str.slice(0, str.length - 1);
      return str;
    },
    //跳转到详情页
    goDetailPage(params, router, type) {
      const { href } = this.$router.resolve({
        name: router,
        query: {
          shiftId: params ? params.shiftId : "",
          nextShiftId: params ? params.nextShiftId : "",
          pageType: type ? type : "",
        },
      });
      window.open(href, "_blank");
    },
    //补录
    makeUp() {
      let data = this.$refs.klTable.getRadioData();
      if (data) {
        let params = {
          shiftId: data.thisShiftId,
          nextShiftId: data.nextShiftId,
        };
        this.goDetailPage(params, "handoverDetails", "补录");
      } else {
        this.$message.warning("请先选择一条数据");
      }
    },
    //导出
    download() {
      let paramStr = "";
      let params = this.searchData;
      for (const key in params) {
        if (Object.hasOwnProperty.call(params, key)) {
          if (params[key] && params[key] != "") {
            paramStr += `${key}=${params[key]}&`;
          }
        }
      }
      paramStr = paramStr.substring(0, paramStr.length - 1);
      let path = `eam-service-operation/shift/export?${paramStr}`;
      let fileName = `交接班记录`;
      this.$utils.downloadFile(null, fileName, path);
    },
  },
};
</script>
<style lang="less" scoped>
.overhaulApplication {
  //margin-left: 10px;
}
</style>